package cn.gdcp.listview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by yls on 2018/9/25.
 */

public class MyAdapter extends BaseAdapter{
    private ArrayList<Student> studentArrayList;
    private Context context;

    public MyAdapter(Context context,ArrayList<Student> studentArrayList){
        this.context=context;
        this.studentArrayList=studentArrayList;
    }

    @Override
    public int getCount() {
        return studentArrayList.size(); //获取数量
    }

    @Override
    public Object getItem(int i) {
        return studentArrayList.get(i); //获取学生
    }

    @Override
    public long getItemId(int i) {
        return i; //获取位置
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater layoutInflater=LayoutInflater.from((context));
        view = layoutInflater.inflate(R.layout.my_list_item,viewGroup,false);

        ImageView headView=view.findViewById(R.id.headView);
        headView.setImageResource(studentArrayList.get(i).getImgid());

        TextView nameView=view.findViewById(R.id.nameView);
        nameView.setText(studentArrayList.get(i).getName());

        TextView ageView=view.findViewById(R.id.ageView);
        ageView.setText(String.valueOf(studentArrayList.get(i).getAge()));

        return view;
    }
}
